package com.jason.campusinn.view;

import android.widget.*;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.yuandian.wanna.R;
import com.yuandian.wanna.activity.navigationDrawer.OrderMeasureActivity;
import com.yuandian.wanna.bean.initialize.LoginInfo;
import com.yuandian.wanna.bean.navigationDrawer.MeasureStoreBean;
import com.yuandian.wanna.constants.InterfaceConstants;
import com.yuandian.wanna.utils.CommonMethodUtils;
import com.yuandian.wanna.utils.HttpRequestCallBack;
import com.yuandian.wanna.utils.HttpUtil;
import com.yuandian.wanna.utils.LogUtil;
import com.yuandian.wanna.wheelview.ArrayWheelAdapter;
import com.yuandian.wanna.wheelview.CityModel;
import com.yuandian.wanna.wheelview.DistrictModel;
import com.yuandian.wanna.wheelview.OnWheelChangedListener;
import com.yuandian.wanna.wheelview.ProvinceModel;
import com.yuandian.wanna.wheelview.WheelView;
import com.yuandian.wanna.wheelview.XmlParserHandler;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by Victor on 2015/8/10.
 */
public class StorePopupWindow extends PopupWindow implements OnWheelChangedListener {

    private View view;
    private Activity mContext;
    private WheelView mViewStores;
    private Button btn_save;
    private MeasureStoreBean bean;
    private int mselectedIndex;

    //所有门店
    protected String[] mStoresData;
    //当前门店
    protected String mCurrentStore;

    public StorePopupWindow(Activity context) {
        this.mContext = context;

        //初始化页面
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.popup_store_address, null);
        setContentView(view);
        btn_save = (Button) view.findViewById(R.id.provice_pop_btn);
        mViewStores = (WheelView) view.findViewById(R.id.id_store);
        mViewStores.addChangingListener(this);
        initStoreData();

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderMeasureActivity.STORE_ADDRESS = bean.getReturnData().get(mselectedIndex).getStoreAddress();
                OrderMeasureActivity.STORE_ID = bean.getReturnData().get(mselectedIndex).getStoreId();
                OrderMeasureActivity.hasSelectedStore = true;
                dismiss();
            }
        });

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popupStyle);
        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xB0FFFFFF);
        // 设置SelectPicPopupWindow弹出窗体的背景
//        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        this.update();

//        setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                showSelectedResult();
//            }
//        });
    }


    private void initStoreData() {
        HttpUtil.sendJsonRequest(HttpRequest.HttpMethod.GET,
                InterfaceConstants.BASE_URL + "public/stores",
                "", new HttpRequestCallBack<Object>() {
                    @Override
                    public void onSuccessed(ResponseInfo<Object> responseInfo) {
                        bean = new Gson().fromJson(responseInfo.result.toString(), MeasureStoreBean.class);
                        mStoresData = new String[bean.getReturnData().size()];
                        for (int i = 0; i < bean.getReturnData().size(); i++) {
                            mStoresData[i] = bean.getReturnData().get(i).getStoreName();
                        }
                        mViewStores.setViewAdapter(new ArrayWheelAdapter<String>(mContext, mStoresData));
                        // 设置可见条目数量
                        mViewStores.setVisibleItems(5);
                        LogUtil.d(responseInfo.toString());
                    }

                    @Override
                    public void onFailed(HttpException exception, String reason) {
                        Toast.makeText(mContext, "获取门店数据失败，请稍后重试", Toast.LENGTH_SHORT).show();
                        LogUtil.d(reason);
                    }
                }, 0);
    }

    public String getmCurrentStore() {
        return mCurrentStore;
    }

    public void setmCurrentStore(String mCurrentStore) {
        this.mCurrentStore = mCurrentStore;
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewStores) {
            mCurrentStore = mStoresData[newValue];
            mselectedIndex = newValue;
        }
    }
}


